//
//  MineInfoImageTableViewCell.m
//  MJApp
//
//  Created by scjh on 15/3/27.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineInfoImageTableViewCell.h"
#import "SMPageControl.h"
#import "MineInfoImageCellCollectionViewCell.h"

@interface MineInfoImageTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *imageCollectionView;
@property (weak, nonatomic) IBOutlet SMPageControl *pageControl;

@end

@implementation MineInfoImageTableViewCell


+ (instancetype)initWithTableView:(UITableView *)tableView {
    
    static NSString *cellID = @"MineInfoImageTableViewCell";
    MineInfoImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        
        cell =  WL_XIB(@"MineInfoImageTableViewCell");
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell UI];
    }
    cell.cellHigh = CGRectGetHeight(cell.frame);
    return cell;
    
}

- (void)UI {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.imageCollectionView.collectionViewLayout = flowLayout;
    self.imageCollectionView.dataSource = self;
    self.imageCollectionView.delegate = self;
    self.imageCollectionView.pagingEnabled = YES;
    self.imageCollectionView.backgroundColor = Color_Back_Black;
    [self.imageCollectionView registerNib:[UINib nibWithNibName:@"MineInfoImageCellCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MineInfoImageCellCollectionViewCell"];
    
    
    self.pageControl.currentPage = 0;
    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    
}

- (void)setPhotos:(NSArray *)photos {
    
    _photos = photos;
    self.pageControl.numberOfPages = photos.count;
    [self.imageCollectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.photos.count > 0 ? self.photos.count : 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MineInfoImageCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineInfoImageCellCollectionViewCell" forIndexPath:indexPath];
    if (self.photos.count > 0) {
       
        cell.infoDic = self.photos[indexPath.row];
    }
    cell.backgroundColor = Color_Back_Black;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGSize size = CGSizeZero;
    size.width = CGRectGetWidth(self.imageCollectionView.frame);
    size.height = CGRectGetHeight(self.imageCollectionView.frame);
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
        
        [self.delegate homeCell:self withInfo:@{@"index":@(indexPath.row)}];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    self.pageControl.currentPage = scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame);
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
